﻿<?php
/**
 * 用于管理登录请求处理页面
 *
 * @author kinet <380511703@qq.com>
 * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
 */
session_start();
require_once( 'path.php' );
require_once( SOJ_ROOT.'/include/function/lib.php' );

//若用户已经登录,则重定向到欢迎界面
if( !empty($_SESSION['admin']) ){

 	$_SESSION['success_message'] = 'Welcome back , '.$_SESSION['username'];
	header( 'Location:../manager/manager_index.php' );
	exit ;
}

@$request = $_POST;
//此处应由request对象负责处理将要传递给
//user_sign_process对象的键值不应有
//captcha,因为对于captcha的判断只存在于
//本页面 .
//@see sign.php
@$captcha_value = $request['captcha_value'] ;
unset( $request['captcha_value'] ) ;

//cookies的处理,同样的cookies也不会传递给
//user_sign_process对象,对cookies的处理也
//仅仅限于本页面.
if ( !empty( $request['set_cookie'] ) ){

	//cookie保存一周时间(604800s)
	setcookie( "cookie_admin" , $request['admin'] , time()+604800 );
	unset( $request['set_cookie'] ) ;
}
//判断验证码是否正确
if ( !empty($_SESSION['captcha_value']) ){

	if ( $_SESSION['captcha_value'] != $captcha_value ){

		$_SESSION['errors'] = 'Invalid captcha value.';
		header('Location:manager_login.php');
		exit ;
	}
}
if( empty($request['admin']) || empty($request['passwd']) ){

	$_SESSION['errors'] = '	Please provide both a username, and a password.';
	header('Location:manager_login.php');
	exit ;
}

@$admin = $request['admin'] ;
@$passwd = $request['passwd'] ;

try{
	$admin_login_process = new admin_login_process( db_connect() , $request );

	if ($admin_login_process->do_signin() ){

		//登录成功

		//清除$_SESSION['errors']避免该用户
		//在关闭浏览器前登录时再次出现验证码提示
		//@see sign.php
		unset( $_SESSION['errors'] ) ;
		unset( $_SESSION['captcha_value'] );
		unset( $_SESSION['captcha_path_and_file_name'] );

		//传递给sign_success页面的信息,
		//提示用户登录成功
		$_SESSION['success_message'] = "Sign in success";
		header('Location:../manager/manager_login.php');
		exit ;

	}else{

		$_SESSION['errors'] = 'Invalid username or password.';
		header('Location:manager_login.php');
		exit ;
	}

}catch(Exception $e){

	 exception_handle( $e ) ;
}
